<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

	$error_code = 1;
	$error_description = 'Ошибка. Не указан ID голосования.';
	
	header("Content-Type: text/xml; charset=utf-8");
	$dom = new DOMDocument('1.0','UTF-8');
	$responce = $dom->createElement('responce');

	if( isset($_POST['q_id']) && is_numeric($_POST['q_id']) && isset($_POST['a_id']) ){
		
		$q_id = $_POST['q_id'];
		$error_code = 0;
		
		require($MSO->config['plugins_dir'] . 'samborsky_polls/result.php');
		sp_write_logs();
		
		
		// Ответ
		$responce->appendChild($dom->appendChild($dom->createElement('response',sp_result($q_id))));
		$dom->appendChild($responce);
	}	
	
	// Ошибка
	$responce->appendChild($dom->appendChild($dom->createElement('error_status',$error_code)));
	$dom->appendChild($responce);

	// Код ошибки
	$responce->appendChild($dom->appendChild($dom->createElement('error_description',$error_description)));
	$dom->appendChild($responce);

	echo $dom->saveXML();
	
	function sp_write_logs(){
		global $MSO;
		
		$CI = &get_instance();
		
		$host = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
		$ip = ip2long($_SERVER['REMOTE_ADDR']);
		
		foreach( $_POST['a_id'] as $a_id ){
			
			$CI->db->insert('sp_logs',array(
				'l_qid' 		=> $_POST['q_id'],
				'l_aid' 		=> $a_id,
				'l_ip'			=> $ip,
				'l_host'		=> $host,
				'l_timestamp'	=> gmmktime(),
				'l_userid'		=> is_login() ? $MSO->data['session']['users_id'] : 0,
				'l_user'		=> is_login() ? $MSO->data['session']['users_login'] : '-'
			));
		}
		
	}	
	
?>